﻿package mortal.game.view.mainUI.roleAvatar
{
    import Message.Public.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.cd.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.menu.*;
    import mortal.game.view.group.panel.*;
    import mortal.game.view.mainUI.avatar.*;
    import mortal.mvc.core.*;

    public class GroupAvatar extends PlayerAvatar
    {
        protected var _publicPlayer:SPublicPlayer;
        protected var _captainIcon:GBitmap;
        protected var _stateIcon:GBitmap;
        protected var _kitOutTimerData:SecTimerViewData;
        protected var _chatTimerData:SecTimerViewData;
        protected var _chatBox:GroupChatBox;

        public function GroupAvatar()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.addCaptain();
            this.addStateIcon();
            this.configEventListener(MouseEvent.CLICK, this.setTarget);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._captainIcon.dispose(param1);
            this._captainIcon = null;
            this._stateIcon.dispose(param1);
            this._stateIcon = null;
            this.removeChatBox();
            this.removeSecTimer();
            super.disposeImpl(param1);
            return;
        }// end function

        override protected function addLookPlayerBtn() : void
        {
            return;
        }// end function

        private function setTarget(event:MouseEvent) : void
        {
            if (event.target != _name)
            {
                if (_entityInfo)
                {
                    ThingUtil.selectEntity = ThingUtil.entityUtil.getEntity(_entityInfo.entityInfo.entityId);
                }
            }
            return;
        }// end function

        protected function addStateIcon() : void
        {
            this._stateIcon = UIFactory.gBitmap(null, 12, 7, this);
            return;
        }// end function

        protected function addCaptain() : void
        {
            this._captainIcon = UIFactory.gBitmap(ImagesConst.SmallCatain, -12, -15, this);
            return;
        }// end function

        override protected function addBg() : void
        {
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.AvatarEntityBg, 0, 0, this));
            return;
        }// end function

        override protected function addName() : void
        {
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.size = 12;
            _name = UIFactory.gTextField("", 60, -10, 95, 20, this, _loc_1, false);
            return;
        }// end function

        override protected function addLifeBar() : void
        {
            var _loc_1:* = new GTextFormat();
            _loc_1.size = 8;
            _loc_1.align = AlignMode.CENTER;
            _shengmingBar = UICompomentPool.getUICompoment(BaseProgressBar);
            _shengmingBar.createDisposedChildren();
            _shengmingBar.setLabel(BaseProgressBar.ProgressBarTextPercent, 5, -2, 80, 10, _loc_1);
            _shengmingBar.setProgress(ImagesConst.AvatarEntityLife, false, 3, 1, 89, 8);
            _shengmingBar.x = 54;
            _shengmingBar.y = 11;
            this.addChild(_shengmingBar);
            return;
        }// end function

        override protected function addMana() : void
        {
            var _loc_1:* = new GTextFormat();
            _loc_1.size = 8;
            _loc_1.align = AlignMode.CENTER;
            _mofaBar = UICompomentPool.getUICompoment(BaseProgressBar);
            _mofaBar.createDisposedChildren();
            _mofaBar.setLabel(BaseProgressBar.ProgressBarTextPercent, 5, -2, 80, 10, _loc_1);
            _mofaBar.setProgress(ImagesConst.AvatarEntityMana, false, 3, 1, 86, 8);
            _mofaBar.x = 54;
            _mofaBar.y = 20;
            this.addChild(_mofaBar);
            return;
        }// end function

        override protected function addAvatar() : void
        {
            _avatar = UIFactory.gImageBitmap("", 3, -29, _bgSprite);
            return;
        }// end function

        override public function updateLevel(event:Event = null) : void
        {
            if (_entityInfo)
            {
                _level.text = "Lv. " + _entityInfo.entityInfo.level;
                return;
            }
            if (this._publicPlayer)
            {
                _level.text = "Lv. " + this._publicPlayer.level;
            }
            return;
        }// end function

        override public function updateLife(event:Event = null) : void
        {
            if (!_entityInfo)
            {
                _shengmingBar.setValue(0, 0);
                return;
            }
            if (_shengmingBar.value > _entityInfo.entityInfo.life)
            {
                EffectManager.glowFilterReg(this, [FilterConst.colorGlowFilter(16711680)], 1, 13, 1, 1);
            }
            _shengmingBar.setValue(_entityInfo.entityInfo.life, _entityInfo.entityInfo.maxLife);
            return;
        }// end function

        override public function updateMana(event:Event = null) : void
        {
            if (!_entityInfo)
            {
                _mofaBar.setValue(0, 0);
                return;
            }
            _mofaBar.setValue(_entityInfo.entityInfo.mana, _entityInfo.entityInfo.maxMana);
            return;
        }// end function

        override public function updateName(event:Event = null) : void
        {
            if (_entityInfo)
            {
                _name.htmlText = "<a href=\'event:#\'>" + _entityInfo.entityInfo.name + "</a>";
            }
            else if (this._publicPlayer)
            {
                _name.htmlText = "<a href=\'event:#\'>" + this._publicPlayer.name + "</a>";
            }
            return;
        }// end function

        override public function updateCarrer() : void
        {
            var _loc_1:int = 0;
            if (_entityInfo)
            {
                _loc_1 = _entityInfo.entityInfo.career;
            }
            else if (this._publicPlayer)
            {
                _loc_1 = this._publicPlayer.career;
            }
            if (_loc_1)
            {
                _carrer.bitmapData = GlobalClass.getBitmapData(GameDefConfig.instance.getECarrerSmallPic(_loc_1));
            }
            return;
        }// end function

        protected function updateCaptain() : void
        {
            if (!this._publicPlayer)
            {
                return;
            }
            if (EntityUtil.equal(this._publicPlayer.entityId, Cache.instance.group.captain))
            {
                this._captainIcon.visible = true;
            }
            else
            {
                this._captainIcon.visible = false;
            }
            return;
        }// end function

        protected function addSecTimer() : void
        {
            if (!this._kitOutTimerData)
            {
                this._kitOutTimerData = new SecTimerViewData();
            }
            this._kitOutTimerData.setLeftTime(300);
            this._kitOutTimerData.timeOutCaller = this.kitOutPlayer;
            return;
        }// end function

        protected function removeSecTimer() : void
        {
            if (this._kitOutTimerData)
            {
                this._kitOutTimerData.dispose();
                this._kitOutTimerData = null;
            }
            return;
        }// end function

        public function updateOnline() : void
        {
            if (this._publicPlayer.online == 0)
            {
                this._stateIcon.bitmapData = GlobalClass.getBitmapData(ImagesConst.Offline);
                _shengmingBar.setValue(0, 100);
                _mofaBar.setValue(0, 100);
                _buffPanel.clean();
                this.addSecTimer();
            }
            else
            {
                this.removeSecTimer();
            }
            return;
        }// end function

        private function kitOutPlayer() : void
        {
            if (Cache.instance.group.isCaptain && !CopyUtil.isInGroupCopy)
            {
                if (this._publicPlayer)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GroupKickOut, this._publicPlayer.entityId));
                }
            }
            return;
        }// end function

        public function updateLeave() : void
        {
            if (this._publicPlayer.online == 1)
            {
                this._stateIcon.bitmapData = GlobalClass.getBitmapData(ImagesConst.leave);
                _shengmingBar.setValue(0, 100);
                _mofaBar.setValue(0, 100);
                _buffPanel.clean();
            }
            return;
        }// end function

        protected function updateDead(event:Event = null) : void
        {
            if (!_entityInfo)
            {
                return;
            }
            if (_entityInfo.entityInfo.life <= 0)
            {
                this._stateIcon.bitmapData = GlobalClass.getBitmapData(ImagesConst.dead);
            }
            else
            {
                this._stateIcon.bitmapData = null;
            }
            return;
        }// end function

        public function updatePlayer(param1:SPublicPlayer) : void
        {
            this._publicPlayer = param1;
            if (_entityInfo == null)
            {
                this.updateCarrer();
                this.updateName();
                this.updateLevel();
                this.updateLeave();
            }
            this.updateCaptain();
            this.updateOnline();
            this.updateAvatar();
            var _loc_2:* = GameMapConfig.instance.getMapInfo(this._publicPlayer.mapId);
            if (_loc_2)
            {
                _bgSprite.toolTipData = _loc_2.name;
            }
            if (entityInfo == null)
            {
                PlayerMenuRegister.UnRegister(_bgSprite);
                PlayerMenuRegister.Register(_bgSprite, this._publicPlayer, PlayerMenuConst.GroupMemberOpMenu);
            }
            return;
        }// end function

        override public function updateEntity(param1:EntityInfo) : void
        {
            super.updateEntity(param1);
            PlayerMenuRegister.UnRegister(_bgSprite);
            PlayerMenuRegister.Register(_bgSprite, param1, PlayerMenuConst.GroupMemberOpMenu);
            if (param1 == null)
            {
                _avatar.filters = [FilterConst.colorFilter2];
            }
            else
            {
                _avatar.filters = [];
                this._stateIcon.bitmapData = null;
            }
            this.updateDead();
            return;
        }// end function

        override public function stopUpdateEntity() : void
        {
            super.stopUpdateEntity();
            _avatar.filters = [FilterConst.colorFilter2];
            PlayerMenuRegister.UnRegister(_bgSprite);
            PlayerMenuRegister.Register(_bgSprite, this._publicPlayer, PlayerMenuConst.GroupMemberOpMenu);
            if (this._publicPlayer.online == 1)
            {
                this._stateIcon.bitmapData = GlobalClass.getBitmapData(ImagesConst.leave);
            }
            return;
        }// end function

        public function removeAndStopUpdate() : void
        {
            this.stopUpdateEntity();
            this.removeSecTimer();
            PlayerMenuRegister.UnRegister(_bgSprite);
            this._publicPlayer = null;
            return;
        }// end function

        override protected function addEntityEvent() : void
        {
            super.addEntityEvent();
            if (_entityInfo)
            {
                _entityInfo.addEventListener(EntityInfoEventName.UpdateLife, this.updateDead);
            }
            return;
        }// end function

        override protected function removeEntityEvent(event:EntityInfoEvent = null) : void
        {
            super.removeEntityEvent();
            if (_entityInfo)
            {
                _entityInfo.removeEventListener(EntityInfoEventName.UpdateLife, this.updateDead);
            }
            return;
        }// end function

        override public function updateAvatar() : void
        {
            if (this._publicPlayer)
            {
                _avatar.imgUrl = AvatarUtil.getPlayerAvatarUrl(this._publicPlayer.career, this._publicPlayer.sex, AvatarUtil.Mid);
            }
            else if (_entityInfo)
            {
                _avatar.imgUrl = AvatarUtil.getPlayerAvatarUrl(_entityInfo.entityInfo.career, _entityInfo.entityInfo.sex, AvatarUtil.Mid);
            }
            return;
        }// end function

        public function updateMapId(param1:int) : void
        {
            this._publicPlayer.mapId = param1;
            _bgSprite.toolTipData = GameMapConfig.instance.getMapInfo(this._publicPlayer.mapId).name;
            return;
        }// end function

        public function get publicPlayer() : SPublicPlayer
        {
            return this._publicPlayer;
        }// end function

        public function addChatData(param1:String) : void
        {
            if (this._chatBox == null)
            {
                this._chatBox = UICompomentPool.getUICompoment(GroupChatBox);
                this._chatBox.move(140, 5);
                this.addChild(this._chatBox);
            }
            this._chatBox.chatTxt = param1;
            this.addTimerData();
            return;
        }// end function

        private function addTimerData() : void
        {
            if (this._chatTimerData == null)
            {
                this._chatTimerData = new SecTimerViewData();
                this._chatTimerData.timeOutCaller = this.removeChatBox;
                this._chatTimerData.setLeftTime(3);
            }
            else
            {
                this._chatTimerData.reset();
            }
            return;
        }// end function

        private function removeChatBox() : void
        {
            if (this._chatBox)
            {
                this._chatBox.dispose();
                this._chatBox = null;
            }
            if (this._chatTimerData)
            {
                this._chatTimerData.dispose();
                this._chatTimerData = null;
            }
            return;
        }// end function

    }
}
